package com.wyl.mobileplayer.ui.activity;

import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;

import com.wyl.mobileplayer.R;
import com.wyl.mobileplayer.utils.ToastUtils;

/**
 * 时间：2018/6/26 0026 14:09
 * 描述：系统播放器
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author WangYoule
 * @qq 270628297
 */
public class SystemVideoPlayer extends AppCompatActivity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {

    private static final String TAG = "Look";

    private VideoView videoView;

    private Uri mUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate: ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_video_player);
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        //准备好的监听
        videoView.setOnPreparedListener(this);
        //播放出错的监听
        videoView.setOnErrorListener(this);
        //播放完成的监听
        videoView.setOnCompletionListener(this);
    }

    private void initData() {
        mUri = getIntent().getData();
        videoView.setVideoURI(mUri);
    }

    private void initView() {
        videoView = findViewById(R.id.videoView);
        videoView.setMediaController(new MediaController(this));
    }

    @Override
    protected void onStart() {
        Log.d(TAG, "onStart: ");
        super.onStart();
    }

    @Override
    protected void onResume() {
        Log.d(TAG, "onResume: ");
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.d(TAG, "onPause: ");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.d(TAG, "onStop: ");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "onDestroy: ");
        super.onDestroy();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        Log.d(TAG, "onSaveInstanceState: ");
        super.onSaveInstanceState(outState);
    }


    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        Log.d(TAG, "onRestoreInstanceState: ");
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        Log.d(TAG, "onConfigurationChanged: newConfig = " + newConfig);
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        videoView.start();
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        ToastUtils.showLongToast(this, "播放出错了噢");
        return false;
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        ToastUtils.showLongToast(this, "播放完成了" + mUri);
    }
}
